package week5.day33;

import java.util.HashMap;

/**
 * @Author fengzihao
 * @Date 2024-05-17 19:33
 */
public class TwoSum {
    public static void main(String[] args) {
        int[] nums = new int[]{2, 7, 11, 15};
        int target = 9;
        int[] result = twoSum(nums,target);
        System.out.println("["+result[0]+","+result[1]+"]");
    }

    private static int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> integerIntegerHashMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if (integerIntegerHashMap.containsKey(complement))
                return new int[]{integerIntegerHashMap.get(complement),i};
            integerIntegerHashMap.put(nums[i], i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }
}
